<?php

namespace Admin\Controller;
use User\Api\UserApi;
use Admin\Model;

/**
 * 购物车查询
 * @author Weiliqiang <email address>
 */
class CartController extends AdminController 
{

    /**
     * 购物车列表
     * @author Weiliqiang <[<email address>]>
     */
    public function showcart()
    {
        // 获取排序条件
        $content = null != (string)(I('request.sort')) ? (string)(I('request.sort')) : 'desc';
        $order['id'] = $content;

        // 获取搜索条件，封装成$map数组
        $username         =   I('username');
        $map['a.status']  =   array('egt',0);
        if(is_numeric($username)){
            $map['a.goods_name|b.username']=   array(intval($username),array('like','%'.$username.'%'),'_multi'=>true);
        }else{
            $map['a.goods_name|b.username']    =   array('like', '%'.(string)$username.'%');
        }

        // 计算商品列表的商品总数
        $sql = "select count(*) total from north_cart where status>=0";
        $total = D('cart')->query($sql);
        $total = $total[0]['total'];

        // 获取每页显示的条数
        $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;

        // 实例化分页类，传入总数和每页显示条数
        $page = new \Think\Page($total, $listRows);

        $data = D('cart')->alias('a')->join('north_user b ON a.user_id=b.user_id')->limit($page->firstRow.','.$page->listRows)->where($map)->order($order)->field("a.id,a.goods_name,a.size,a.num,a.price,a.image,a.type,a.color,a.status,a.ctime,b.username")->select();

        // 把查询出的一些数据进行必要的解析
        $status = ['未付款' , '已付款'];
        // print_r($data);exit;
        foreach($data as &$v){
          $v['status'] = $status[$v['status']];
          $v['ctime'] = date('Y/m/d',$v['ctime']);
        }

        // 如果查询总数大于每页显示的条数，则显示分页
        if($total>$listRows){
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p =$page->show();
        $this->assign('_page', $p? $p: '');
        $this->assign('data' , $data);
        $this->display();
    }

}
